#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() {
	int n;
	scanf("%d", &n);
	int  p[100] = { 0 }, arr[500] = { 0 };
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	int des;
	scanf("%d", &des);
	int child;
	for ( child = n - 1; child >= 0; child--) {
		if (arr[child] == des) {
			break;
		}
	}
	int i = 0;
	while (child > 0) {
		p[i++] = arr[child];
		child=(child - 1) / 2;
	}
	p[i++] = arr[child];
	while (--i>0) {
		printf("%d ", p[i]);
	}
	return 0;
}